<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="教材名称" prop="textbookName">
        <el-input v-model="form.textbookName" placeholder="请输入教材名称" />
      </el-form-item>
      <el-form-item label="教材类型" prop="typeIds">
        <el-cascader
          v-model="form.typeIds"
          :options="options"
          @change="handleChange"
          :props="props"
        ></el-cascader>
        <!-- <el-input v-model="form.typeId" placeholder="请选择教材类型" /> -->
      </el-form-item>

      <el-form-item label="适用层次" prop="hierarchy">
        <el-select v-model="form.hierarchy" placeholder="请选择教室层级">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.STUDENT_TRAINING_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否公开" prop="open">
        <el-select v-model="form.open" placeholder="请选择是否公开">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_NUMBER)"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发版时间" prop="publicationTime">
        <el-date-picker
          clearable
          v-model="form.publicationTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择发版时间"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="form.author" placeholder="请输入作者" />
      </el-form-item>
      <el-form-item label="出版社" prop="press">
        <el-input v-model="form.press" placeholder="请输入出版社" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="LOGO" prop="logoImage">
        <image-upload v-model="form.logoImage" :limit="1" />
        <!-- <el-input v-model="form.logoImage" placeholder="请输入logo" /> -->
      </el-form-item>

      <el-form-item label="简介" prop="synopsis">
        <editor v-model="form.synopsis" :min-height="192" />
        <!-- <el-input v-model="form.synopsis" placeholder="请输入简介" /> -->
      </el-form-item>
      <!-- <el-form-item label="机构编号" prop="orgId">
                        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
                      </el-form-item>
                      <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注" />
                      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  createTextbookInfo,
  updateTextbookInfo,
  textbookTypeTree
} from "@/api/collegebase/textbookInfo";
import Editor from "@/components/Editor";
import ImageUpload from "@/components/ImageUpload";
export default {
  name: "TextbookInfoEdit",
  components: {
    Editor,
    ImageUpload,
  },
  data() {
    return {
      props: {
        value: "id",
        label: "typeName",
        children: "sonList",
        checkStrictly: true
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeIds: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        textbookName: [
          { required: true, message: "教材名称不能为空", trigger: "blur" },
        ],
        hierarchy: [
          { required: true, message: "适用层次不能为空", trigger: "blur" },
        ],
        open: [
          { required: true, message: "是否公开不能为空", trigger: "change" },
        ],
        publicationTime: [
          { required: true, message: "发版时间不能为空", trigger: "blur" },
        ],
        logoImage: [
          { required: true, message: "LOGO 图片不能为空", trigger: "blur" },
        ],
        press: [{ required: true, message: "出版社不能为空", trigger: "blur" }],
      },
      options: [],
      data: [],
    };
  },
  created() {
    this.getTree();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },
  methods: {
    handleChange() {

    },
    getTree() {
      textbookTypeTree().then((res) => {
        this.options = res.data;

      });
    },
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.typeIds = [...this.form.parentIds.reverse(), this.form.typeId];
        this.form.datetimerange = [this.form.startTime, this.form.endTime];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        typeId: undefined,
        textbooxName: undefined,
        hierarchy: undefined,
        open: undefined,
        publicationTime: undefined,
        logoImage: undefined,
        press: undefined,
        phone: undefined,
        author: undefined,
        synopsis: undefined,
        orgId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        this.form.typeId = this.form.typeIds.at(-1);
        // this.form.logoImage = 'noImageUploadUrl'
        // 修改的提交
        if (this.form.id != null) {

          updateTextbookInfo(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createTextbookInfo(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
